#include<iostream>
#include<unistd.h>
#include<vector>
#include<cstdlib>
#include<functional>
#include<sys/types.h>
#include<sys/wait.h>
#include "task.h"
using namespace std;

typedef function<void()> task_t;

int main()
{
  vector<task_t> tasks;
  tasks.push_back(PrintLog);
  tasks.push_back(DownLoad);
  tasks.push_back(BackUp);

  pid_t id = fork();
  if(id == 0)
  {
    while(1)
    {
      cout<<"我是子进程,pid："<<getpid()<<endl;
      sleep(1);
    }
    exit(0);
  }
  //father
  while(true)
  {
    sleep(1);
    pid_t rid = waitpid(id,nullptr,WNOHANG);
    if(rid>0)
    {
      cout<<"子进程"<< rid  <<"等待成功"<<endl;
      break;
    }
    else if(rid < 0)
    {
      cout<<"子进程等待失败"<<endl;
      break;
    }
    else
    {
      cout<<"子进程尚未终止"<<endl;
      //做自己的事
      for(auto& task : tasks)
        task();
    }
  }
  return 0;
}
